package org.jadestudio.home.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jadestudio.home.dao.CommonDao;
import org.jadestudio.home.service.ArticleService;
import org.jadestudio.home.service.GameService;

public class InterfaceAction extends DispatchAction {
	private GameService gameService;
	private ArticleService articleService;
	
	public GameService getGameService() {
		return gameService;
	}
	public void setGameService(GameService gameService) {
		this.gameService = gameService;
	}
	
	public ArticleService getArticleService() {
		return articleService;
	}
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
	
	public InterfaceAction(){
		
	}
	
	/**
	 * 获取当月游戏发售表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getGameRetailList(ActionMapping mapping, ActionForm form,
	        HttpServletRequest request,
	        HttpServletResponse response) throws Exception{
		try
		{
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8"); 
			
			int year = Integer.parseInt(request.getParameter("year"));
			int month = Integer.parseInt(request.getParameter("month"));
			List<java.util.Map> map=gameService.getGameRetailList(year, month);
			// 输出JSON
			PrintWriter out = response.getWriter();

			StringBuffer sb = new StringBuffer();
			sb.append("{");
			sb.append("\"success\":\"true\",");
			sb.append("\"retail\":[");
			for(int i=0;i<map.size();i++)
			{
				sb.append("{" +
						"\"date\":"+ map.get(i).get("date") + "," +
						"\"id\":\""+ map.get(i).get("game_id") +"\"," +
						"\"name\":\""+ map.get(i).get("simplified_chinese_name") +"\"" +
						"}");
				if (i<map.size()-1) sb.append(",");
			}
			
			sb.append("]}");
			
			out.write(sb.toString());
			out.flush();
			out.close();
		}
		catch(Exception ex)
		{
			PrintWriter out = response.getWriter();
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8"); 

			out.write("{\"success\":\"false\"}");
			out.flush();
		}
		
		return null;
	}
	
	
	/**
	 * 为文章投票
	 */
	public ActionForward voteArticle(ActionMapping mapping, ActionForm form,
	        HttpServletRequest request,
	        HttpServletResponse response) throws Exception{
		try
		{
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8"); 
			
			String article_id = request.getParameter("article_id");
			int value = Integer.parseInt(request.getParameter("value"));
			String ip = CommonDao.getIP(request);
			int result= articleService.voteArticle(article_id, value, ip);
			// 输出JSON
			PrintWriter out = response.getWriter();

			StringBuffer sb = new StringBuffer();
			sb.append("{");
			if (result==1){
				sb.append("\"success\":\"true\"");
			}else{
				sb.append("\"success\":\"false\",");
				if (result==-1){
					sb.append("\"info\":\"您已评分，请勿重复评分。\"");
				}else{
					sb.append("\"info\":\"评分失败，请稍后再试。\"");
				}
			}			
			sb.append("}");
			
			out.write(sb.toString());
			out.flush();
			out.close();
		}
		catch(Exception ex)
		{
			PrintWriter out = response.getWriter();
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8"); 

			out.write("{\"success\":\"false\",\"info\":\"评分失败，请稍后再试。\"}");
			out.flush();
		}
		
		return null;
	}
	
	
	/**
	 * 为游戏投票
	 */
	public ActionForward voteGame(ActionMapping mapping, ActionForm form,
	        HttpServletRequest request,
	        HttpServletResponse response) throws Exception{
		try
		{
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8"); 
			
			String game_id = request.getParameter("game_id");
			int value = Integer.parseInt(request.getParameter("value"));
			String ip = CommonDao.getIP(request);
			int result= gameService.voteGame(game_id, value, ip);
			// 输出JSON
			PrintWriter out = response.getWriter();

			StringBuffer sb = new StringBuffer();
			sb.append("{");
			if (result==1){
				sb.append("\"success\":\"true\"");
			}else{
				sb.append("\"success\":\"false\",");
				if (result==-1){
					sb.append("\"info\":\"您已评分，请勿重复评分。\"");
				}else{
					sb.append("\"info\":\"评分失败，请稍后再试。\"");
				}
			}			
			sb.append("}");
			
			out.write(sb.toString());
			out.flush();
			out.close();
		}
		catch(Exception ex)
		{
			PrintWriter out = response.getWriter();
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8"); 

			out.write("{\"success\":\"false\",\"info\":\"评分失败，请稍后再试。\"}");
			out.flush();
		}
		
		return null;
	}
	
	
	/**
	 * 获取游戏下载链接
	 */
	public ActionForward getGameDownloads(ActionMapping mapping, ActionForm form,
	        HttpServletRequest request,
	        HttpServletResponse response) throws Exception{
		try
		{
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8"); 
			
			String id = request.getParameter("id");
			List<java.util.Map> result = gameService.getGameDownloads(id);
			// 输出JSON
			PrintWriter out = response.getWriter();

			StringBuffer sb = new StringBuffer();
			if (result!=null){
				sb.append("{\"success\":\"true\",\"list\":[");
				for(int i=0;i<result.size();i++){
					sb.append("{\"url\":\""+result.get(i).get("game_downloads_url")+"\",");
					sb.append("\"name\":\""+result.get(i).get("game_downloads_name")+"\"}");
					if (i<result.size()-1) sb.append(",");
				}
				sb.append("]}");
			}
			else
			{
				sb.append("{\"success\":\"false\"}");
			}
			
			out.write(sb.toString());
			out.flush();
			out.close();
		}
		catch(Exception ex)
		{
			PrintWriter out = response.getWriter();
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8"); 

			out.write("{\"success\":\"false\"}");
			out.flush();
		}
		
		return null;
	}
	
	
	/**
	 * 获取游戏购买链接
	 */
	public ActionForward getGamePurchases(ActionMapping mapping, ActionForm form,
	        HttpServletRequest request,
	        HttpServletResponse response) throws Exception{
		try
		{
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8"); 
			
			String id = request.getParameter("id");
			List<java.util.Map> result = gameService.getGamePurchases(id);
			// 输出JSON
			PrintWriter out = response.getWriter();

			StringBuffer sb = new StringBuffer();
			if (result!=null){
				sb.append("{\"success\":\"true\",\"list\":[");
				for(int i=0;i<result.size();i++){
					sb.append("{\"pic_url\":\""+result.get(i).get("game_purchase_pic_url")+"\",");
					sb.append("\"price\":\""+result.get(i).get("game_purchase_price")+"\",");
					sb.append("\"title\":\""+result.get(i).get("game_purchase_title").toString().replace('\"','\'')+"\",");
					sb.append("\"url\":\""+result.get(i).get("game_purchase_url")+"\",");					
					sb.append("\"store\":\""+result.get(i).get("game_purchase_store_name")+"\"}");
					if (i<result.size()-1) sb.append(",");
				}
				sb.append("]}");
			}
			else
			{
				sb.append("{\"success\":\"false\"}");
			}
			
			out.write(sb.toString());
			out.flush();
			out.close();
		}
		catch(Exception ex)
		{
			PrintWriter out = response.getWriter();
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8"); 

			out.write("{\"success\":\"false\"}");
			out.flush();
		}
		
		return null;
	}	
}
